/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Users;
import healthcare.coursework.service.UserService;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zkplus.databind.BindingListModelList;

import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Window;

/**
 *
 * @author Administrator
 */
public class TrackStaff extends GenericAutowireComposer {

    //Componennt
    private Combobox nameSearch;
    private Button staffQuery;
    private Listbox staffDataListView;
    private Users selected;
    //Service
    private UserService userService = util.Utility.getUtility().getUserService();
    private Paging patientPaging;
    private List<Users> staffs;

    private void setInfo() {
        doFilter();
        nameSearch.setModel(new ListModelList(staffs, true));
    }

    public void onStaffQuery(Event event) {
        doFilter();
    }

    public void onLoadList(Event event) {
        setInfo();
    }

    public void onNewStaff(Event event) {
        try {
            Window win = (Window) Executions.createComponents(
                    "/WEB-INF/zul/pages/adminStaff/AddStaff.zul", null, null);
            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void onEditStaff(Event event) {
        try {
            Map params = new HashMap();
            params.put("user", (Users) staffDataListView.getSelectedItem().getValue());
            System.out.println("Edit: " + ((Users) params.get("user")).getUsername());
            Window win = (Window) Executions.createComponents(
                    "/WEB-INF/zul/pages/adminStaff/EditStaff.zul", null, params);

            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public void onStatusStaff(Event event) {

        try {
            int status = Messagebox.show("Are you sure to block staffs?", "Question", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION, new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (((Integer) event.getData()).intValue() == Messagebox.YES) {
                        System.out.println("Enter remove method");
                        Iterator it = staffDataListView.getSelectedItems().iterator();
                        System.out.println(staffDataListView.getSelectedItems().size());
                        while (it.hasNext()) {
                            Listitem li = (Listitem) it.next();
                            Users user = (Users) li.getValue();
                            if(user.getEnabled())
                                user.setEnabled(false);
                            else
                                user.setEnabled(true);
                            userService.edit(user, null);
                        }
                        doFilter();
                        return;
                    } else {
                        System.out.println("Messagebox.No selected!");
                        return;
                    }
                }
            });



        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private void doFilter() {
        try {
            if (nameSearch.getText() == null || nameSearch.getText().trim().equals("")) {
                staffs = userService.findUsers();
            } else {
                String query = nameSearch.getText();
                staffs = userService.findUsersByFullName(query);
                for (int i = 0; i < staffs.size(); i++) {
                    Users users = staffs.get(i);
                    System.out.println(users.getUsername());
                }
            }
        } catch (Exception e) {
            staffs = userService.findUsers();
        }
        filterAdmin();
        staffDataListView.setModel(new BindingListModelList(staffs, true));

    }

    public void filterAdmin() {
        for (int i = 0; i < staffs.size(); i++) {
            Users user=staffs.get(i);
            if (user.getListAuthorities().equals("Adminstrative")) {
                staffs.remove(user);
            }
        }
    }
}
